var maxProfit = function (prices) {
  const n = prices.length;
  let dp0 = 0, dp1 = -prices[0];
  for (let i = 1; i < n; ++i) {
    let newDp0 = Math.max(dp0, dp1 + prices[i]);
    // 0 -> 4 -> 4 -> 7 -> 7
    let newDp1 = Math.max(dp1, dp0 - prices[i]);
    // -1 -> -1 -> 1 -> 1 -> 3
    dp0 = newDp0;
    dp1 = newDp1;
  }
  return dp0;
};

console.log(maxProfit([7, 1, 5, 3, 6, 4]));